package org.yejiali.service.impl;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.codec.Base16Codec;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.yejiali.common.enums.AppHttpCodeEnum;
import org.yejiali.common.response.ResponseResult;
import org.yejiali.domain.entity.User;
import org.yejiali.domain.vo.UserInfoVo;
import org.yejiali.exception.SystemException;
import org.yejiali.mapper.UserMapper;
import org.yejiali.service.UserService;
import org.yejiali.util.SecurityUtils;

/**
 * 用户表(User)表服务实现类
 *
 * @author yejiali
 * @since 2025-02-28 20:10:26
 */
@Service("userService")
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    /**
     * 获取当前用户信息
     *
     * @return
     */
    @Override
    public ResponseResult userInfo() {
        Long userId = SecurityUtils.getUserId();
        User user = getById(userId);
        UserInfoVo userVo = BeanUtil.copyProperties(user, UserInfoVo.class);
        userVo.setId(user.getCreateBy());
        return ResponseResult.okResult(userVo);
    }

    /**
     * 修改当前用户信息
     *
     * @param user
     * @return
     */

    @Override
    public ResponseResult updateUserInfo(User user) {
        updateById(user);
        return ResponseResult.okResult();
    }

    /**
     * 注册用户信息
     *
     * @param user
     * @return
     */
    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    public ResponseResult register(User user) {
        //这里的判断为空也可交给参数校验去做
        //判断用户名是否为空
        if(!StringUtils.hasText(user.getUserName())){
            throw new SystemException(AppHttpCodeEnum.REQUIRE_USERNAME); //抛出封装的系统异常
        }
        //判断密码是否为空(可以对密码进行格式校验)
        if(!StringUtils.hasText(user.getPassword())){
            throw new SystemException(AppHttpCodeEnum.PASSWORD_NOT_NULL);
        }
        //判断昵称是否为空
        if(!StringUtils.hasText(user.getNickName())){
            throw new SystemException(AppHttpCodeEnum.REQUIRE_NICKNAME);
        }
        //可以加上判断邮箱是否可以为空，这里默认邮箱可为空
        //查看用户名,邮箱是否和数据库中重复(注意要分别判断)
        if(userNameExist(user.getUserName())){
           throw new SystemException(AppHttpCodeEnum.USERNAME_EXIST);
        }
        if(userEmailExist(user.getEmail())){
            throw new SystemException(AppHttpCodeEnum.EMAIL_EXIST);
        }
        //保存用户信息，密码加密(这里需要注入用来加密对象）
        user.setPassword(passwordEncoder.encode(user.getPassword()));
        save(user);
        return ResponseResult.okResult();
    }

    private boolean userEmailExist(String email) {
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(User::getEmail,email);
        return count(lambdaQueryWrapper)>0;
    }
    private boolean userNameExist(String userName) {
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(User::getUserName,userName);
        return count(lambdaQueryWrapper)>0;
    }
}


